package com.lsfb.signln.My.ChangePwd;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.lsfb.signln.Login.LoginActivity;
import com.lsfb.signln.R;
import com.lsfb.signln.Utils.LittleUtils;
import com.lsfb.signln.Utils.URLString;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.HashMap;

/**
 * Created by Administrator on 2018/1/25 0025.
 */
@ContentView(R.layout.aty_change_pwd)
public class ChangePwdActivity extends LsfbActivity {

    /**
     * 旧密码
     */
    @ViewInject(R.id.aty_change_pwd_old)
    private EditText change_pwd_old;

    /**
     * 新密码
     */
    @ViewInject(R.id.aty_change_pwd_new)
    private EditText change_pwd_new;

    /**
     * 确认密码
     */
    @ViewInject(R.id.aty_change_pwd_again)
    private EditText change_pwd_again;

    @Override
    public void initView() {
        LittleUtils.setsimpletitlebar(this, "修改密码");
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_change_post})
    public void onClick(View view) {
        switch (view.getId()) {
            //提交
            case R.id.aty_change_post:
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(change_pwd_old.getWindowToken(), 0);
                imm.hideSoftInputFromWindow(change_pwd_new.getWindowToken(), 0);
                imm.hideSoftInputFromWindow(change_pwd_again.getWindowToken(), 0);

                upDatePwd();
                break;
        }
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {

    }

    /**
     * 修改密码
     */
    public void upDatePwd() {
        if (change_pwd_old.getText().toString().isEmpty()) {
            T.showShort(this, "请输入旧密码");
            return;
        }
        if (change_pwd_new.getText().toString().isEmpty()) {
            T.showShort(this, "请输入新密码");
            return;
        }
        if (change_pwd_again.getText().toString().isEmpty()) {
            T.showShort(this, "请输入确认密码");
            return;
        }
        if (change_pwd_new.getText().toString().length() < 6 || change_pwd_new.getText().toString().length() > 9) {
            T.showShort(this, "请输入6~9位字符的密码");
            return;
        }
        if (!change_pwd_again.getText().toString().equals(change_pwd_new.getText().toString())) {
            T.showShort(this, "两次密码输入不一致，请重新输入");
            change_pwd_again.setText("");
            change_pwd_new.setText("");
            return;
        }

        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("oldpwd", change_pwd_old.getText().toString());
        hashMap.put("currentpwd", change_pwd_new.getText().toString());
        new BaseInternet().getData(URLString.UPDATEPWD, hashMap, String.class, new ChangePwdEvent(), true);
    }

    @EventAnnotation
    public void ChangePwdEvent(ChangePwdEvent event) {
        switch (event.getCode()) {
            case 200:
                T.showShort(this, "修改成功");
                finish();
                break;
            case 1003:
                T.showShort(this, "原密码错误");
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

}
